/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package com.googlecode.chigs.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

import org.synyx.hades.domain.AbstractPersistable;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
@Entity
@NamedQueries({@NamedQuery(name = "ConfigRevision.findByAll", query = "from ConfigRevision o"),
	@NamedQuery(name = "ConfigRevision.findByConfigurationLatest", query = "from ConfigRevision o where o.configuration = ? and o.nr = (select max(rev.nr) from ConfigRevision rev where rev.configuration = o.configuration)"),
	@NamedQuery(name = "ConfigRevision.findByModule", query = "select o from ConfigRevision o, in (o.revisions) r where r.module = ?")})
public class ConfigRevision extends AbstractPersistable<Long> {
	private int nr;
	private String description;

	@ManyToOne
	private Version version;
	
	@ManyToOne
	private Configuration configuration;
	
	@ManyToOne
	private User user;

	@OneToMany(mappedBy="configRevision")
	private List<ConfigValue> configValues;

	@ManyToMany
    @JoinTable(name="ConfigRevision_Revision",
            joinColumns=@JoinColumn(name="configRevision_id", referencedColumnName="id"),
            inverseJoinColumns=@JoinColumn(name="revision_id", referencedColumnName="id"))
	private List<Revision> revisions;

	public ConfigRevision() {
		this.description = "";
		this.version = new Version();
		this.configuration = new Configuration();
		this.user = new User();
		this.configValues = new ArrayList<ConfigValue>();
		this.revisions = new ArrayList<Revision>();
	}
	
	public int getNr() {
		return nr;
	}

	public void setNr(int nr) {
		this.nr = nr;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Version getVersion() {
		return version;
	}

	public void setVersion(Version version) {
		this.version = version;
	}

	public Configuration getConfiguration() {
		return configuration;
	}

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}

	public List<ConfigValue> getConfigValues() {
		return configValues;
	}

	public void setConfigValues(List<ConfigValue> configValues) {
		this.configValues = configValues;
	}

	public List<Revision> getRevisions() {
		return revisions;
	}

	public void setRevisions(List<Revision> revisions) {
		this.revisions = revisions;
	}

	@Override
	public String toString() {
		return String
				.format(
						"ConfigRevision [configuration=%s, description=%s, nr=%s, user=%s, version=%s, getId()=%s]",
						configuration, description, nr, user, version, getId());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((configValues == null) ? 0 : configValues.hashCode());
		result = prime * result
				+ ((configuration == null) ? 0 : configuration.hashCode());
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + nr;
		result = prime * result
				+ ((revisions == null) ? 0 : revisions.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		result = prime * result + ((version == null) ? 0 : version.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (!(obj instanceof ConfigRevision)) {
			return false;
		}
		ConfigRevision other = (ConfigRevision) obj;
		if (configValues == null) {
			if (other.configValues != null) {
				return false;
			}
		} else if (!configValues.equals(other.configValues)) {
			return false;
		}
		if (configuration == null) {
			if (other.configuration != null) {
				return false;
			}
		} else if (!configuration.equals(other.configuration)) {
			return false;
		}
		if (description == null) {
			if (other.description != null) {
				return false;
			}
		} else if (!description.equals(other.description)) {
			return false;
		}
		if (nr != other.nr) {
			return false;
		}
		if (revisions == null) {
			if (other.revisions != null) {
				return false;
			}
		} else if (!revisions.equals(other.revisions)) {
			return false;
		}
		if (user == null) {
			if (other.user != null) {
				return false;
			}
		} else if (!user.equals(other.user)) {
			return false;
		}
		if (version == null) {
			if (other.version != null) {
				return false;
			}
		} else if (!version.equals(other.version)) {
			return false;
		}
		return true;
	}
}
